﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;

namespace BookSystem.User
{
    public partial class UserRegister : Form
    {
        public UserRegister()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = SQLHelper.ExecuteTable("select COUNT(*)+1 from T_user");
            String Uid = "1";

            if(dt.Rows.Count > 0){

                Uid = dt.Rows[0][0].ToString().Trim();
            
            }

            model.User u = new model.User(Uid, UserName.Text, Pwd.Text);

            if (UserName.Text != String.Empty && !BLL.User.isEquals(UserName.Text))
            {

                if (BLL.User.addUser(u))
                {
                    UserName.Clear();
                    Pwd.Clear();
                    MessageBox.Show("注册成功！");
                    message.Text = String.Empty;

                }
                else
                {

                    MessageBox.Show("注册失败！");
                    message.Text = String.Empty;
                }

            }
            else {

                MessageBox.Show("用户名不能为空！");
            }
            

        }

        private void UserName_Leave(object sender, EventArgs e)
        {
            if (UserName.Text != String.Empty)
            {
                if (BLL.User.isEquals(UserName.Text))
                {
                    message.ForeColor = Color.Red;
                    message.Text = "用户名重复！";

                }
                else
                {

                    message.ForeColor = Color.Green;
                    message.Text = "用户名可以使用！";

                }


            }
            else {

                message.ForeColor = Color.Red;
                message.Text = "用户名不能为空！";
            
            }
            
        }
    }
}
